# -*- coding: UTF-8 -*-
from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoise

from Config.setting import settings
from Apps.user.user_routes import router as user_routes
from Apps.mitm.mitm_router import router as mitm_router
from loguru import logger
from Config.setting import LOG_DIR
from Utilts.logger import LoggingMiddleware

import datetime
import uvicorn

app = FastAPI(title=settings.app_name)
app.add_middleware(LoggingMiddleware)

timestamp = datetime.datetime.now().timestamp()  # 获取当前时间戳
date_str = datetime.datetime.fromtimestamp(timestamp).strftime('%Y%m%d%H')  # 获取每小时
logger.add(f"{LOG_DIR}/{date_str}.log", level="INFO", rotation="10 MB", format="{time} - {level} - {message}")

register_tortoise(
    app,
    db_url=settings.TORTOISE_ORM,
    modules={"models": ["Apps.user.user_models"]},
    generate_schemas=True,
    add_exception_handlers=True,
)

# 添加 user 的路由
app.include_router(user_routes)
app.include_router(mitm_router)

if __name__ == '__main__':
    uvicorn.run("main:app", host="127.0.0.1", port=8005, reload=True)
